//app.js
const key = 'OSUBZ-J57WO-5CQW5-SOOIL-LN64H-FYF6H'
const QQMapWX = require('./utils/qqmap-wx-jssdk.min.js')
let qqmapsdk = new QQMapWX({key: key});

App({
  onLaunch() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.setServerUrl();
    
    this.loginFun();
    


  },
  isweek(date){
    let day = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    return day[new Date(date).getDay()]
  },
  //发送用户信息到page
  sendUserInfoPage(Callback){
    if (this.globalData.userInfo) {
      Callback(this.globalData.userInfo) 
    }else{
      this.loginFun();
    }
  },


  //发送用户信息到服务器

  sendUserInfoServer(userInfo){
    let self = this;
    wx.request({
      url: this.globalData.serverUrl+'wx/login.do', 
      data: {
        userinfo: userInfo ,
        code: this.globalData.longinCode
      },
      success: function(res) {
        if (res.data) {
          self.globalData.serverOpenInfo = res.data
          self.getServerUserInfo()

        }
      }
    })
  },
  // 获取服务器用户信息
  getServerUserInfo(){
    let self = this;
    wx.request({
      url: self.globalData.serverUrl+'myMessage/queryMyMessage.do', 
      data: {
        openId: self.globalData.serverOpenInfo.openid
      },
      success: function(res) {
        if (res.data.state == 1) {
          self.globalData.userInfo = res.data.data
        }

      }
    })
  },

  // 获取用户信息
  getUserInfoFun(){
    let self = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            lang: 'zh_CN',
            success: res => {
              // console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              this.sendUserInfoServer(res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{
         wx.authorize({
          scope: 'scope.userInfo',
          success() {
            self.getUserInfoFun();
          }
        })
       }
     }
   })
  },
    // 登录
    loginFun(userinfo){
      let self = this;
      wx.login({
        success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        self.globalData.longinCode = res.code;
        self.getUserInfoFun();

      }
    })
    },
    //推送地理信息到server
    sendAddressServer(Address){
      wx.request({
        url: this.globalData.serverUrl +'wx/userLoginlog.do',
        data:{
          gpsAddress: Address
        },
        success(res){
          console.log(res)
        }
      })
    },
  //解析地址信息
  reverseGeocoder(callBack){
    let self = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        let latitude = res.latitude
        let longitude = res.longitude
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function(res) {
            self.globalData.address = res;
            if (callBack) {callBack(res)}
              self.sendAddressServer(res);
          },
          fail: function(res) {
            console.log(res);
          },
          complete: function(res) {

          }
        });
      }
    })

  },
  //获取地址信息
  getAddress(callBack){
    if (this.globalData.address) {
      callBack(this.globalData.address)
    }else{
      this.reverseGeocoder(callBack);
    }
  },
  setServerUrl(){
    if (this.globalData.debug) {
      this.globalData.serverUrl =  'http://192.168.199.45:8080/Carpooling/'
    }else{
      this.globalData.serverUrl = 'https://xcx.feefox.cn/'
    }
  },
  globalData: {
    debug: false,
    longinCode: null,
    serverOpenInfo: null,
    userInfo: null,
    serverUrl: null,
    address:null
  }
})